package com.chb.entity;

import cn.hutool.core.collection.CollUtil;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author CHB
 * @createTime 2022-6-16 11:06
 */
@Data
public class SecurityUserInfo {

    private Long userId;

    private String name;

    private String pwd;

    private Integer sex;

    private String phone;

    private List<String> permissionList;

    /**
     * 封装权限信息，供security鉴权
     * @author CHB
     * @date 2022-08-24 09:39:29
     * @return List<GrantedAuthority>
     */
    public Collection<GrantedAuthority> getAuthorities(){
        List<GrantedAuthority> authorities = new ArrayList<>();
        if(CollUtil.isNotEmpty(permissionList)){
            for (String permission : permissionList) {
                SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(permission);
                authorities.add(simpleGrantedAuthority);
            }
        }
        return authorities;
    }
}
